package list.p1_arraylist;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Test {

    public static void test1() {
        MyArrayList list = new MyArrayList();
//        list.addAll(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(10);
        list.add(4);
        list.add(5);
        list.add(8);
        list.add(7);
        list.add(9);
        list.add(6);
        list.add(11);
        list.add(12, 2);
        list.foreach(o -> System.out.print(o + " "));
        System.out.println();
        list.forEach(o -> System.out.print(o + " "));
        System.out.println();
        list.sort((o1, o2) -> o1 - o2);
        System.out.println(list);
        list.sort(Comparator.comparingInt(o -> o));
        list.sort((o1, o2) -> o2 - o1);
        System.out.println(list);
        System.out.println(list.getCapacity());
        System.out.println(list.getSize());
        list.stream().forEach(o -> System.out.print(o + " "));
        System.out.println();
        System.out.println(list.get(1));
        list.set(12, 1);
        System.out.println(list.get(1));
        System.out.println(list.contains(1));
        System.out.println(list.find(3));
    }

    public static int removeDuplicates(int[] nums) {
        int len = nums.length;
        int k = 0;
        int temp = nums[0];
        for (int i = 1; i < len; i++) {
            if (nums[i] != temp) {
                nums[k++] = temp;
                temp = nums[i];
            }
        }
        nums[k++] = temp;
        return k;
    }

    public static void merge(int[] nums1, int m, int[] nums2, int n, int[] arr) {
        int i = 0, j = 0, k = 0;
        while (i < m && j < n) {
            if (nums1[i] < nums2[j]) {
                arr[k++] = nums1[i++];
            } else if (nums1[i] > nums2[j]) {
                arr[k++] = nums2[j++];
            } else {
                arr[k++] = nums2[j++];
                arr[k++] = nums1[i++];
            }
        }

        while (i < m) {
            arr[k++] = nums1[i++];
        }

        while (j < n) {
            arr[k++] = nums2[j++];
        }
    }

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int i = m - 1, j = n - 1, k = m + n - 1;
        while (i >= 0 && j >= 0) {
            if (nums1[i] > nums2[j]) {
                nums1[k--] = nums1[i--];
            } else if (nums1[i] < nums2[j]) {
                nums1[k--] = nums2[j--];
            } else {
                nums1[k--] = nums1[i--];
                nums1[k--] = nums2[j--];
            }
        }

        while (i >= 0) {
            nums1[k--] = nums1[i--];
        }

        while (j >= 0) {
            nums1[k--] = nums2[j--];
        }
    }

    public static void merge2(int[] nums1, int m, int[] nums2, int n) {
        int k = m-- + n-- - 1;
        while(n >= 0) {
            nums1[k--] = m >= 0 && nums1[m] >= nums2[n] ? nums1[m--] : nums2[n--];
        }
    }

    public static void show(int[] arr) {
        for (int j : arr) {
            System.out.print(j + " ");
        }
        System.out.println();
    }

    public static void test3() {
//        int[] arr = {1, 2, 3, 4};
//        int[] brr = {1, 2, 2, 3, 4, 5, 6};
//        int[] crr = new int[arr.length + brr.length];
//        merge(arr, 4, brr, 7, crr);
//        show(crr);

        int[] arr = {1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0};
        int[] brr = {1, 2, 2, 3, 4, 5, 6};
        int[] crr = new int[arr.length + brr.length];
        merge(arr, 4, brr, 7);
        merge2(arr, 4, brr, 7);
        show(arr);
    }

    public static void test2() {
        int[] arr = new int[]{1, 1, 2, 2, 3, 3};
        int k = removeDuplicates(arr);
        System.out.println(k);
        Arrays.stream(arr).limit(k).forEach(o -> System.out.print(o + " "));
    }

    public static void test4() {
        boolean b = true ? false : true == true ? false : true;
        System.out.println(b);
    }

    public static String charSet(String str) {
        if (str == null) return null;
        int[] tab = new int[52];
        for (int i = 0; i < str.length(); ++i) {
            if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {
                tab[str.charAt(i) - 'A'] = 1;
            } else {
                tab[str.charAt(i) - 'a' + 26] = 1;
            }

        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < tab.length; i++) {
            if (tab[i] == 1 && i >= 26) {
                sb.append((char) ('a' + i - 26));
            } else if (tab[i] == 1) {
                sb.append((char) ('A' + i));
            }
        }
        return sb.toString();
    }

    public static void test5() {
        System.out.println(charSet("abcasregftrAgaectrvyuUVGBKBGVKesLUIYGBKIUycveryubsukrcikunskrbcbb"));
    }

    public static List<List<Integer>> generate(int numRows) {
        List<List<Integer>> ret = new ArrayList<>();

        List<Integer> list0 = new ArrayList<>();
        list0.add(1);
        ret.add(list0);

        for (int i = 1; i < numRows; ++i) {
            List<Integer> curRow = new ArrayList<>();
            curRow.add(1);

            List<Integer> prevRow = ret.get(i - 1);

            for (int j = 1; j < i; ++j) {
                curRow.add(prevRow.get(j - 1) + prevRow.get(j));
            }

            curRow.add(1);
            ret.add(curRow);
        }
        return ret;
    }
    public static void test6() {
        List<List<Integer>> lists = generate(5);
        System.out.println(lists);
    }

    public static void main(String[] args) {
//        test1();
//        test2();
//        test3();
//        test4();
//        test5();
        test6();
    }
}
